﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Inovout.Cloud.DataStore.Utility
{
    public class StringUtility
    {
        /// <summary>
        /// 比较两个字符串是否相等
        /// </summary>
        /// <param name="str1">字符串1</param>
        /// <param name="str2">字符串2</param>
        /// <param name="ignoreCase">是否忽略大小写，默认是忽略</param>
        /// <returns>true：相等；false：不相等</returns>
        public static bool IsEqual(string str1, string str2, bool ignoreCase = true)
        {
            if (str1 == null || str2 == null)
            {
                return false;
            }

            if (String.Compare(str1.Trim(), str2.Trim(), ignoreCase) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断（xxxx-xx）(例如：2013-12)格式的日期字符串是否在两个日期字符串之间
        /// </summary>
        /// <param name="date"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static bool IsBetween(string date, string startDate, string endDate)
        {
            return IsDate1SmallThanDate2(startDate, date) && IsDate1SmallThanDate2(date, endDate);
        }

        /// <summary>
        /// 比较两个（xxxx-xx）(例如：2013-12)日期的字符串的大小
        /// </summary>
        /// <param name="date1">日期1</param>
        /// <param name="date2">日期2</param>
        /// <returns></returns>
        public static bool IsDate1SmallThanDate2(string date1, string date2)
        {
            if (String.IsNullOrEmpty(date1) && String.IsNullOrEmpty(date2))
            {
                return false;
            }

            if (String.IsNullOrEmpty(date1) && !String.IsNullOrEmpty(date2))
            {
                return true;
            }

            if (!String.IsNullOrEmpty(date1) && String.IsNullOrEmpty(date2))
            {
                return false;
            }

            if (String.Compare(date1.Trim(), date2.Trim()) <= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断第一个字符串中是否包含第二个字符串
        /// </summary>
        /// <param name="baseStr">第一个字符串</param>
        /// <param name="subStr">第二个字符串</param>
        /// <param name="ignoreCase">否忽略大小写，默认是忽略</param>
        /// <returns>true：包含；false：不包含</returns>
        public static bool IsContains(string baseStr, string subStr, bool ignoreCase = true)
        {
            if (baseStr == null || subStr == null)
            {
                return false;
            }

            if (ignoreCase)
            {
                return baseStr.Trim().ToUpper().Contains(subStr.Trim().ToUpper());
            }
            else
            {
                return baseStr.Trim().Contains(subStr.Trim());
            }
        }

        /// <summary>
        /// 连接字符串
        /// </summary>
        /// <param name="obj">多个字符串</param>
        /// <param name="connector">连接符</param>
        /// <returns>拼接后的字符串</returns>
        public static string ConnectString(Object[] param, string connector = "")
        {
            if (param == null || param.Length == 0)
            {
                return null;
            }

            StringBuilder sb = new StringBuilder();
            foreach (Object obj in param)
            {
                if (obj != null)
                {
                    sb.Append(connector).Append(obj.ToString());
                }
            }

            string result = sb.ToString();
            if ("" != connector.Trim() && result.StartsWith(connector))
            {
                result = result.Substring(1);
            }

            return result;
        }

        /// <summary>
        /// 根据长度截取字符串
        /// </summary>
        /// <returns></returns>
        public static string GetShotString(string aimString, int nNum)
        {
            int nByteNumber = nNum * 2;
            string strMore = "...";
            int iBytesLength = System.Text.Encoding.Default.GetBytes(aimString).Length;

            if (iBytesLength > nByteNumber)
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0, szNum = 0; i < nByteNumber && szNum < aimString.Length; szNum++)
                {
                    if (Encoding.Default.GetBytes(new char[] { aimString[szNum] }).Length >= 2)
                    {
                        i = i + 2;
                    }
                    else
                    {
                        i = i + 1;
                    }
                    builder.Append(aimString[szNum]);
                }
                return builder.ToString() + strMore;
            }
            else
            {
                return aimString;
            }
        }

        /// <summary>
        /// 去除字符串中的超链接
        /// </summary>
        /// <param name="content">字符串内容</param>
        /// <param name="changestring">要替换超链接的文本（如果不想有超链接就直接匹配#）</param>
        /// <returns></returns>
        public static string GetStrWithoutUrl(string content, string changestring)
        {
            string regularexpression = @"https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?";
            Regex regex = new Regex(regularexpression);
            string str = regex.Replace(content, changestring);
            return str;
        }
    }
}